package com.gec.it.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gec.it.entity.Depts;
import com.gec.it.entity.Doctors;
import com.gec.it.entity.Jobs;
import com.gec.it.service.DeptsService;
import com.gec.it.service.DoctorsService;
import com.gec.it.service.JobsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wf
 * @since 2022-01-05
 */
@Controller
//@RequestMapping("/doctors")
public class DoctorsController {
    @Autowired
    private DoctorsService service = null;

    @Autowired
    private JobsService sservice = null;

    @Autowired
    private DeptsService dservice = null;

    @GetMapping("/login")
    public String login(){
        return "login";
    }

    @PostMapping("/login")
    public String login(String uname, String pwd, Model m, HttpSession session){
        QueryWrapper<Doctors> qw = new QueryWrapper<Doctors>();
        qw.lambda().eq(Doctors::getDocLoginname, uname).eq(Doctors::getDocPwd,pwd);
        Doctors ds = service.getOne(qw);
        if (ds!=null){
            session.setAttribute("ds",ds);
            return "redirect:index";
        }
        m.addAttribute("err","您的账号密码有误！");
        return "login";
    }

    @GetMapping("/index")
    public String index(){
        return "index";
    }

    @GetMapping("/doc_list")
    public String doc_list(Model m){
        List<Doctors> doctors = service.list2();
        m.addAttribute("list",doctors);
        return "doc_list";
    }


    @GetMapping("/doc_add")
    public String doc_add(Model m){
        List<Jobs> jobs = sservice.list();
        List<Depts> list = dservice.list();
        m.addAttribute("jobs",jobs);
        m.addAttribute("dlist",list);
        return "doc_add";
    }

    @PostMapping("/doc_add")
    public String doc_add(Doctors d, MultipartFile myfile, HttpServletRequest req) throws IOException {
        if (!myfile.isEmpty()){
            //重命名图片
            String filename = myfile.getOriginalFilename();
            String ext = filename.substring(filename.lastIndexOf("."));//获取后缀.jpg .png
            String newFilename = UUID.randomUUID().toString()+ext;
            //定位保存路径

            String imgpath = req.getServletContext().getInitParameter("doc_imgs");//读取配置
            String webapps = new File(req.getServletContext().getRealPath("/")).getParent();//获取Tomcat的webapps目录路径
            //自动创建文件夹
            File dir = new File(webapps+imgpath);
            if (!dir.exists()){
                dir.mkdirs();
            }
            myfile.transferTo(new File(webapps+imgpath+"/"+newFilename));
            d.setDocPhoto(newFilename);
        }
        System.out.println(d);
        service.save(d);
        return "redirect:doc_list";
    }

    @GetMapping("/doc_edit")
    public String doc_edit(int id,Model m){
        Doctors byId2 = service.getById2(id);
        List<Depts> list = dservice.list2();
        List<Jobs> jobs = sservice.list2();
        m.addAttribute("list",byId2);
        m.addAttribute("dlist",list);
        m.addAttribute("jlist",jobs);
        return "doc_edit";
    }

    @PostMapping("/doc_edit")
    public String doc_edit(int id,Doctors doc){
        doc.setDocId(id);
        service.updateById(doc);
        return "redirect:doc_list";
    }

    @GetMapping("/doc_del")
    public String doc_del(int id){
        service.removeById(id);
        return "redirect:doc_list";
    }
}
